import Vue from 'vue'
import Vuex from 'vuex'
import { getItem, setItem } from '@/utils/stroage'

Vue.use(Vuex)

const USER_KEY = 'toutiao-user'

export default new Vuex.Store({
  state: {
    // user:JSON.parse(window.localStorage.getItem('user')) //当前登录状态 （token）
    user: getItem(USER_KEY),

    cachePages: ['LayoutIndex']
  },
  mutations: {
    setUser(state,data){
      state.user = data
      //防止刷新数据丢失，需要把数据放到本地存储中，持久化数据
      // window.localStorage.setItem('user',JSON.stringify(state.user))
      setItem(USER_KEY, state.user)
    },
    //添加缓存
    addcachePage(state,pageName){
      if(!state.cachePages.includes(pageName)) {
        state.cachePages.push(pageName)
      }
    },
    //移除缓存
    removechePage(state,pageName){
      const index = state.cachePages.indexOf(pageName)
      if(index !== -1) {
        state.cachePages.splice(index,1)
      }
    }
  },
  actions: {
  },
  modules: {
  }
})
